// const modules = import.meta.glob('@/teaching-cases/*/index.ts')
let caseKeysCache: string[] = []

const stringArrToDict = (arr: string[]) => {
    const dict: {
        label: string
        value: string
    }[] = []

    arr.forEach((item) => {
        dict.push({
            label: item,
            value: item,
        })
    })
    return dict
}

// 获取teaching-cases下的所有文件夹名称
export const getTeachCaseKeys = (dict?: boolean) => {
    if (caseKeysCache.length) {
        if (dict) {
            return stringArrToDict(caseKeysCache)
        }
        return caseKeysCache
    }
    // 获取@/teaching-cases的子文件夹名称
    const modules = import.meta.glob('@/teaching-cases/*/index.ts')
    const keys = Object.keys(modules).map((key) => {
        const match = key.match(/src\/teaching-cases\/(.*)\/index\.ts/)
        if (match && match[1] !== '_template') {
            return match[1]
        }
    })
    caseKeysCache = keys.filter(Boolean) as string[]
    if (dict) {
        return stringArrToDict(caseKeysCache)
    }
    return caseKeysCache
}

// 获取events的导入函数
export const getEventsImport = (caseKey: string) => {
    const modules = import.meta.glob('@/teaching-cases/*/core/emitter.ts')
    console.log(modules)
    const importFunc = modules[`/src/teaching-cases/${caseKey}/core/emitter.ts`]
    return importFunc as typeof importFunc | undefined
}
